<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Document extends CI_Model {
    public function __construct() {
    	parent::__construct();
    	$this->load->model('page');
    	$this->load->model('pagedocument');
    	$this->load->model('news');
    }
	    
    public function getDocuments($options = array()) {   
    	$options = _default(array('sortDirection' => 'asc'), $options);
    	$queryFields = array('doc_name', 'doc_description', 'doc_date',
    	                     'doc_path', 'doc_author', 'doc_article',
    	                     'doc_id', 'doc_page', 'doc_type');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit]']);
    	
    	if (isset($options['sortBy']))
    		$this->db->order_by($options['sortBy'],
    		                    $options['sortDirection']);
    	$query = $this->db->get('acs_documents');
    	if ($query->num_rows() == 0)
    		return FALSE;
    	if (isset($options['doc_id']))
    		return $query->row_array(0);
    	else
    		return $query->result_array();     
    }
    
    public function countDocuments($options = array()) {   
    	$queryFields = array('doc_name', 'doc_description', 'doc_date',
    	                     'doc_path', 'doc_author', 'doc_article',
    	                     'doc_id', 'doc_page');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit]'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit]']);
    	
    	$this->db->from('acs_documents');
    	return $this->db->count_all_results();
    }
    
    public function getLast10($options = array())
    {
        $opts = _default(array('sortBy' => 'doc_date', 
                               'sortDirection' => 'desc',
                               'limit' => 10), $options);
        return $this->getDocuments($opts);
    }
    
    public function updateDocument($options = array()) {
		$fields = array('doc_name', 'doc_description', 'doc_date', 
		                'doc_path', 'doc_author', 'doc_detailpage',
		                'doc_id', 'doc_page', 'doc_type', 'doc_owner', 
		                'doc_contact');
		if (!_required(array('doc_id'), $options)) {
			return FALSE;
		}
		foreach ($fields as $field) {
			if (isset($options[$field]))
				$this->db->set($field, $options[$field]);
		}
		$this->db->where('doc_id', $options['doc_id']);
		$this->db->update('acs_documents');
		return $this->db->affected_rows();
    }
    
    public function insertDocument($options = array()) {
		$docid = FALSE;
		$pagid = FALSE;
		$options = _default(array('doc_date' => date('Y-m-d H:i:s')), $options);
		$fields = array('doc_name', 'doc_description', 'doc_date', 
		                'doc_path', 'doc_author', 'doc_detailpage',
		                'doc_page', 'doc_type', 'doc_owner', 
		                'doc_contact');
		if (!_required(array('doc_name', 'doc_description','doc_path'),
		               $options)) {
			return FALSE;
		}
		foreach ($fields as $field) {
			if (isset($options[$field]))
				$this->db->set($field, $options[$field]);
		}
		$this->db->insert('acs_documents');
		$docid = $this->db->insert_id();
		$pagid = $this->page->insertPage(array(
		     'pag_title' => 'S-a publicat: ' . $options['doc_name'],
		     'pag_name' => str_replace(' ', '_', $options['doc_name']),
		     'pag_description' => $options['doc_description']));
		if ($docid != FALSE && $pagid != FALSE) {
			$this->pagedocument->addDocumentToPage(array(
												'dp_document' => $docid,
												'dp_page' => $pagid));
			$this->news->insertNews(array(
		     'new_title' => 'S-a publicat documentul: ' . $options['doc_name'],
		     'new_type' => 'D',
		     'new_objid' => $docid,
             'new_route' => base_url(). $options['doc_path']));
            $this->updateDocument(array('doc_id' => $docid, 
                                        'doc_detailpage' => $pagid));
			return $docid;
		} else {
			return FALSE;
		}
    }
    
    public function deleteDocument($options = array()) {
    	return TRUE;
    }
}
